package com.geline.mybatisplus.interceptor.inner;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SM4;
import com.geline.mybatisplus.handler.DSLockedHandler;

public class SM4FieldHandler implements DSLockedHandler {

    @Override
    public String encryptStr(String secretKey, String text) {
        String newKey = SecureUtil.md5(secretKey);
        //sm4秘钥要求为128bit；对应32位16进制字符
        SM4 sm4 = SmUtil.sm4(HexUtil.decodeHex(newKey));
        return sm4.encryptBase64(text);
    }

    @Override
    public String decryptStr(String secretKey, String text) {
        String newKey = SecureUtil.md5(secretKey);
        //sm4秘钥要求为128bit；对应32位16进制字符
        SM4 sm4 = SmUtil.sm4(HexUtil.decodeHex(newKey));
        return sm4.decryptStr(text);
    }
}
